﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OptimumInfra.Models;
using OptimumInfra.ViewData;

namespace OptimumInfra.Models
{
    public class TemplateRepository
    {

        private OptimumDataContext op = new OptimumDataContext();
        #region template
        public List<ServerTemplate> GetTemplateByCat(int catId)
        {
            return (from a in op.ServerTemplates where a.CategoryId == catId select a).ToList();
        }
        public IEnumerable<ServerTemplate> GetAllTemplates()
        {
            return (from a in op.ServerTemplates select a).ToList();
        }
        public ViewDataTemplate GetTemplateById(int id)
        {
           var result = op.ServerTemplates.SingleOrDefault(a => a.Id == id);
           ViewDataTemplate template = new ViewDataTemplate();
           template.Id = result.Id;
           template.Name = result.Name;
           template.Description = result.Description;
           template.Image = result.Image;
           template.Manufacturer = result.Manufacturer;
           template.Price = result.Price;
           template.CategoryId = result.CategoryId;
           template.Categories = this.GetAllCategories();
           return template;

        }
        public ServerTemplate GetServerTemplateById(int id)
        {
            return op.ServerTemplates.SingleOrDefault(a => a.Id == id);

        }
        public IEnumerable<ServerTemplate> GetTemplatesByCategory(int id)
        {
            return (from a in op.ServerTemplates.Where(a => a.CategoryId == id) select a)
                     .Take(15).ToList();
        }
        public ServerTemplate CreateTemplate(ServerTemplate r)
        {
            op.ServerTemplates.InsertOnSubmit(r);
            op.SubmitChanges();
            return r;
        }
        public void UpdateTemplate(ServerTemplate r)
        {
            var result = op.ServerTemplates.SingleOrDefault(a => a.Id == r.Id);
            result.Name = r.Name;
            result.Image = r.Image;
            result.Manufacturer = r.Manufacturer;
            result.Price = r.Price;
            result.Description = r.Description;
            result.CategoryId = r.CategoryId;
            op.SubmitChanges();
        }

        public void DeleteTemplateById(ServerTemplate r)
        {
            op.ServerTemplates.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        #endregion

        #region templatecategory

        public List<TemplateCategory> GetAllCategories()
        {
            return (from a in op.TemplateCategories select a).ToList();
        }


        public TemplateCategory GetCategoryById(int id)
        {
            return op.TemplateCategories.SingleOrDefault(a => a.Id == id);
        }

        public void CreateCategory(TemplateCategory r)
        {
            op.TemplateCategories.InsertOnSubmit(r);
            op.SubmitChanges();
        }
        public void UpdateTemplate(TemplateCategory r)
        {
            op.SubmitChanges();
        }

        public void DeleteCategoryById(TemplateCategory r)
        {
            op.TemplateCategories.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        #endregion

        #region attribute
        public void DeleteAttributeById(Attribute r)
        {

            op.Attributes.DeleteOnSubmit(r);
            op.SubmitChanges();
        }
        public Attribute GetAttributeById(int id)
        {

            return op.Attributes.Single(a => a.Id == id);
        }

        public Attribute GetResourceAttributeByAmount(int resourceId, int amount)
        {

            var r = from a in op.Attributes where a.Amount == amount select a;
            Attribute attribute = new Attribute();
            foreach (Attribute a in r)
            {
                attribute = a;
                break;
            }
            return attribute;

        }
        #endregion
    }
}